package com.hoshiicloud.user.controller.organization;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.user.entity.organization.ComOrganizationDepartmentes;
import com.hoshiicloud.user.param.organization.ComOrganizationDepartmentesAddParam;
import com.hoshiicloud.user.param.organization.ComOrganizationDepartmentesQueryParam;
import com.hoshiicloud.user.param.organization.ComOrganizationDepartmentesUpdateParam;
import com.hoshiicloud.user.service.organization.ComOrganizationDepartmentesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 租户机构部门 控制器
 * </p>
 *
 * @author campchen
 * @since 2019-05-27
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/organization/com-organization-departmentes")
@Api(value = "SAAS租户供应商部门", tags = {"SAAS租户供应商部门"})
@ApiSort(12)
public class ComOrganizationDepartmentesController {
    @Autowired
    private ComOrganizationDepartmentesService comOrganizationDepartmentesService;

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询租户机构部门
     */
    @ApiOperation(value = "分页查询租户机构部门")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid ComOrganizationDepartmentesQueryParam param) {
        try {
            QueryWrapper<ComOrganizationDepartmentes> queryWrapper = new QueryWrapper<ComOrganizationDepartmentes>();
            queryWrapper.eq(param.getOrganizationId() != null, "organization_id", param.getOrganizationId());
            queryWrapper.eq(param.getParentId() != null, "parent_id", param.getParentId());
            queryWrapper.eq(StringUtils.isNotBlank(param.getName()), "name", param.getName());
            queryWrapper.eq(StringUtils.isNotBlank(param.getOpenPermit()), "open_permit", param.getOpenPermit());
            queryWrapper.eq(StringUtils.isNotBlank(param.getCharge()), "charge", param.getCharge());
            queryWrapper.eq(StringUtils.isNotBlank(param.getIdCard()), "id_card", param.getIdCard());
            queryWrapper.eq(StringUtils.isNotBlank(param.getBankNumber()), "bank_number", param.getBankNumber());
            queryWrapper.eq(StringUtils.isNotBlank(param.getBankName()), "bank_name", param.getBankName());
            queryWrapper.eq(StringUtils.isNotBlank(param.getBankAddress()), "bank_address", param.getBankAddress());
            queryWrapper.eq(StringUtils.isNotBlank(param.getTaxation()), "taxation", param.getTaxation());
            queryWrapper.eq(StringUtils.isNotBlank(param.getPiaohao()), "piaohao", param.getPiaohao());
            queryWrapper.eq(StringUtils.isNotBlank(param.getTelphone()), "telphone", param.getTelphone());
            queryWrapper.eq(StringUtils.isNotBlank(param.getMobile()), "mobile", param.getMobile());
            queryWrapper.eq(StringUtils.isNotBlank(param.getFax()), "fax", param.getFax());
            queryWrapper.eq(StringUtils.isNotBlank(param.getHome()), "home", param.getHome());
            queryWrapper.eq(StringUtils.isNotBlank(param.getEmail()), "email", param.getEmail());
            queryWrapper.eq(StringUtils.isNotBlank(param.getAddress()), "address", param.getAddress());
            Page<ComOrganizationDepartmentes> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString() + ",Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(comOrganizationDepartmentesService.getPage(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询租户机构部门异常", e);
            return Response.failed("分页查询租户机构部门异常");
        }
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询租户机构部门
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询租户机构部门")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(comOrganizationDepartmentesService.getById(id));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增租户机构部门")
    public Response save(@RequestBody @Valid ComOrganizationDepartmentesAddParam param) {
        try {
            ComOrganizationDepartmentes data = BeanUtils.covertType(param, ComOrganizationDepartmentes.class);
            return Response.data(comOrganizationDepartmentesService.insert(data));
        } catch (ServiceException e) {
            log.error("新增租户机构部门异常", e);
            return Response.failed(e.getError().getMsg());
        } catch (Exception e) {
            log.error("新增租户机构部门异常", e);
            return Response.failed();
        }
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改租户机构部门
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改租户机构部门")
    public Response update(@RequestBody @Valid ComOrganizationDepartmentesUpdateParam param) {
        try {
            ComOrganizationDepartmentes data = BeanUtils.covertType(param, ComOrganizationDepartmentes.class);
            return Response.data(comOrganizationDepartmentesService.updateById(data));
        } catch (ServiceException e) {
            log.error("修改租户机构部门异常", e);
            return Response.failed(e.getError().getMsg());
        } catch (Exception e) {
            log.error("修改租户机构部门异常", e);
            return Response.failed();
        }
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 删除租户机构部门
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除租户机构部门")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response delete(@PathVariable("id") Long id) {
        return Response.data(comOrganizationDepartmentesService.deleteById(id));
    }

    /**
     * @Author:campchen
     * @Description: StringUtils.join(ids, " ") 为缓存中添加唯一KEY  如不合适可选择更好方法
     * @Date:上午 10:18 2019/5/11
     * @Param: ids
     * @return: com.hoshiicloud.common.rpc.Response
     */
    @ApiOperation(value = "根据id集合查询租户机构部门")
    @GetMapping("/getByIds")
    public Response getByIds(@RequestParam List<Long> ids) {
        if (CollectionUtils.isNotEmpty(ids)) {
            ids.sort(null);
            return Response.data(comOrganizationDepartmentesService.getByIds(ids, StringUtils.join(ids, "")));
        } else {
            return Response.data(null);
        }
    }


}
